<?php
declare(strict_types=1);

namespace SixShop\MakerBundle\Generator;

use Symfony\Component\Console\Style\SymfonyStyle;

class DirectoryGenerator
{
    /**
     * 生成扩展的基础目录结构
     */
    public function createDirectoryStructure(string $packageName, string $targetPath, SymfonyStyle $io): bool
    {
        try {
            // 获取基础路径
            $basePath = $this->getBasePath($packageName, $targetPath);
            
            // 定义需要创建的目录结构
            $directories = $this->getDirectoryStructure();
            
            $io->section('创建目录结构:');
            
            // 创建所有目录
            foreach ($directories as $dir) {
                $fullPath = $basePath . '/' . $dir;
                if (!$this->createDirectory($fullPath, $io)) {
                    return false;
                }
            }
            
            $io->success('目录结构创建成功!');
            return true;
            
        } catch (\Exception $e) {
            $io->error('创建目录结构时发生错误: ' . $e->getMessage());
            return false;
        }
    }
    
    /**
     * 获取扩展基础路径
     */
    private function getBasePath(string $packageName, ?string $targetPath = null): string
    {
        if ($targetPath !== null) {
            return $targetPath;
        }
        
        // 默认路径
        $vendorDir = dirname(__DIR__, 5); // 从当前文件位置向上5级到vendor上级目录
        return $vendorDir . '/runtime/extension/' . $packageName;
    }
    
    /**
     * 定义扩展的目录结构（参考 six-shop/eav）
     */
    private function getDirectoryStructure(): array
    {
        return [
            'database',
            'database/migrations',
            'route',
            'src',
            'src/Controller',
            'src/Entity',
            'src/Enum',
            'src/Event',
            'src/Hook',
            'src/Job',
            'src/Middleware',
            'src/Model',
            'src/Service',
            'src/Trait',
            'src/Validator',
            'tests',
            'docs'
        ];
    }
    
    /**
     * 创建单个目录
     */
    private function createDirectory(string $path, SymfonyStyle $io): bool
    {
        if (is_dir($path)) {
            $io->text("目录已存在: $path");
            return true;
        }
        
        if (mkdir($path, 0755, true)) {
            $io->text("✓ 创建目录: $path");
            return true;
        } else {
            $io->error("✗ 无法创建目录: $path");
            return false;
        }
    }
    
    /**
     * 检查目录是否已存在
     */
    public function directoryExists(string $packageName, ?string $targetPath = null): bool
    {
        $basePath = $this->getBasePath($packageName, $targetPath);
        return is_dir($basePath);
    }
    
    /**
     * 获取目录结构信息
     */
    public function getDirectoryInfo(string $packageName, ?string $targetPath = null): array
    {
        $basePath = $this->getBasePath($packageName, $targetPath);
        $directories = $this->getDirectoryStructure();
        
        $info = [
            'basePath' => $basePath,
            'directories' => [],
            'exists' => $this->directoryExists($packageName, $targetPath)
        ];
        
        foreach ($directories as $dir) {
            $fullPath = $basePath . '/' . $dir;
            $info['directories'][$dir] = [
                'path' => $fullPath,
                'exists' => is_dir($fullPath)
            ];
        }
        
        return $info;
    }
}